package com.quasar.hpatient.module.comm_album_photo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.comm_album.AlbumFolderBean;
import com.quasar.hpatient.bean.comm_album.AlbumPhotoBean;
import com.quasar.hpatient.widget.StateView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import lib.quasar.base.frame.BasePresenter;
import lib.quasar.context.BaseApp;
import lib.quasar.context.BaseConstant;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashGridLayoutManager;

/**
 * description: 选择图片, 图片集合
 * created by kalu on 2018/4/18 15:54
 */
public class AlbumPhotoPresenter implements BasePresenter {

    private final ArrayList<AlbumPhotoBean> mData = new ArrayList<>();
    private final ArrayList<AlbumPhotoBean> mSelectList = new ArrayList<>();

    void initLayoutData(final AlbumPhotoView view, RecyclerView recycler, boolean hasCamera) {

        if (null == view || null == recycler) return;

        final BaseCommonAdapter<AlbumPhotoBean> adapter = new BaseCommonAdapter<AlbumPhotoBean>(mData, R.layout.fragment_comm_album_photo_child) {

            @Override
            protected void onNext(final RecyclerHolder holder, final AlbumPhotoBean model, int position) {

                final ImageView image = holder.getView(R.id.fragment_comm_album_photo_child_image);

                if (position == 0 && hasCamera) {
                    view.setImage(image, R.drawable.ic_comm_album_camera);
                } else {
                    view.setImage(image, model.getPicture());
                    TextView tip = holder.getView(R.id.fragment_comm_album_photo_child_tip);
                    tip.setVisibility(model.isCheck() ? View.VISIBLE : View.GONE);
                    tip.setText(model.isCheck() ? String.valueOf(mSelectList.indexOf(model) + 1) : BaseConstant.NULL_STR);
                }

                image.setOnClickListener(v -> {

                    if (position == 0 && hasCamera) {
                        view.goCamera();
                    } else {
                        if (model.isCheck()) {
                            mSelectList.remove(model);
                            model.setCheck(false);
                        } else {
                            model.setCheck(true);
                            mSelectList.add(model);
                        }
                        view.refreshList(recycler);
                    }
                });
            }
        };

        StateView loading = (StateView) LayoutInflater.from(BaseApp.getContext()).inflate(R.layout.layout_recycler_loading, null);
        loading.setState(StateView.STATE_LOADING);
        adapter.setNullView(loading);

        final CrashGridLayoutManager manager = new CrashGridLayoutManager(BaseApp.getContext(), 4);
        view.setRecycler(recycler, adapter, manager);
    }

    void photoList(final AlbumPhotoView view, RecyclerView recycler, Map<String, AlbumFolderBean> map, String folder, boolean hasCamera) {

        if (null == view || null == recycler) return;

        mData.clear();
        mSelectList.clear();
        if (hasCamera) {
            AlbumPhotoBean model = new AlbumPhotoBean();
            mData.add(model);
        }

        if (null != map || map.size() > 0) {
            final AlbumFolderBean model = map.get(folder);
            if (null != model) {
                List<AlbumPhotoBean> list = model.getImageList();
                mData.addAll(list);
            }
        }

        view.refreshList(recycler);
    }

    ArrayList<AlbumPhotoBean> getSelectPhoto() {
        return mSelectList;
    }

    @Override
    public void recycler() {
    }
}
